﻿#ifndef IM_Gate_h__
#define IM_Gate_h__

#include <unordered_map>
#include "IM_DataBuffer.h"

class Base_Message;
class Base_Context;
class Base_MessageCore;
class IM_Socket_Server;
class IM_Agent;
struct IM_SocketMessage;

struct connection
{
	int socket_id;
	uint32_t agent;
	uint32_t client;
	IM_DataBuffer buffer;
};

class IM_Gate
{
public:
	IM_Gate();
	~IM_Gate();

	bool init(Base_MessageCore * base_msg_core,IM_Socket_Server *socket_server);
	/*
	通过tcp连接发送数据的

	该函数会在内部 对数据进行打包   并且进行字节序的转换     

	len(2 byte) + data 

	使用的时候并不用关系发送的细节 
	*/
	int  socket_send(int socket_id,const void * buf,int len);
private:

	void _proc(Base_Message * b_msg);
	void _dispatch_socket_message(IM_SocketMessage * sokcet_msg,int sz);
	void _dispatch_message(connection *c, int id, void *data, int size);
	void _forward(connection * c, int size);
	void _accept_new_connection(int socket_id);
	void _ctl(void * msg,int size);
private:
	Base_MessageCore * m_msg_core;
	Base_Context * m_contxt;
	IM_Socket_Server * m_socket_server;
	int  m_listen_socket_id;
	int m_client_id;

	int m_header_size;
	//从socket id 映射到 connect
	std::unordered_map<int,connection *>   m_socketid_map_agent;  
};


#endif // IM_Gate_h__

